﻿using AutoMapper;
using MediatR;
using Sowrk03_28.Repository;
using Swork03_28.Api.Application.Commands.DeepCommand;
using Swork03_28.Domain.TraceableInfos;

namespace Swork03_28.Api.Application.CommandHandlers.DeepCommandHandler
{
    public class DeepFirmCreateCommandHandler : IRequestHandler<DeepFirmCreateCommand, int>
    {
        private readonly IRepository<FirmInfo> _firminfo;
        private readonly IMapper mapper;

        public DeepFirmCreateCommandHandler(IRepository<FirmInfo> firmInfo, IMapper mapper)
        {
            this._firminfo = firmInfo;
            this.mapper = mapper;
        }

        public async Task<int> Handle(DeepFirmCreateCommand request, CancellationToken cancellationToken)
        {
            request.businessCode = (int)FirmType.深加工公司;
            var data = await _firminfo.GetValues(x => x.BatchId == request.batchCode && (int)x.FirmType == request.businessCode);
            var info = data.FirstOrDefault();
            if (info == null)
            {
                var res = mapper.Map<DeepFirmCreateCommand, FirmInfo>(request, info);
                return await _firminfo.CreateAsync(res);
            }
            else
            {
                info = mapper.Map<DeepFirmCreateCommand, FirmInfo>(request, info);
                return await _firminfo.UpdateAsync(info);
            }
        }
    }

    public class DeepFirmSelectCommandHandler : IRequestHandler<DeepFirmSelectCommand, FirmInfo>
    {
        private readonly IRepository<FirmInfo> _firminfo;

        public DeepFirmSelectCommandHandler(IRepository<FirmInfo> firminfo)
        {
            _firminfo = firminfo;
        }

        public async Task<FirmInfo> Handle(DeepFirmSelectCommand request, CancellationToken cancellationToken)
        {
            var info = await _firminfo.GetValues(x => x.BatchId == request.batchCode && x.FirmType == FirmType.深加工公司);
            return info.FirstOrDefault();
        }
    }

    public class DeepFirmImgCreateCommandHandler : IRequestHandler<DeepFirmImgCreateCommand, int>
    {
        private readonly IRepository<FirmInfo> _firminfo;

        public DeepFirmImgCreateCommandHandler(IRepository<FirmInfo> firminfo)
        {
            _firminfo = firminfo;
        }

        public async Task<int> Handle(DeepFirmImgCreateCommand request, CancellationToken cancellationToken)
        {
            var info = await _firminfo.GetValues(x => x.BatchId == request.batchCode && x.FirmType == FirmType.深加工公司);
            var res = info.FirstOrDefault();
            if (res != null)
            {
                res.CampanyImage = (res.CampanyImage + "," + request.imgUrls).TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());
                return await _firminfo.UpdateAsync(res);
            }
            else
            {
                var imgs = ("," + request.imgUrls).TrimStart(",".ToCharArray()).TrimEnd(",".ToCharArray());
                return await _firminfo.CreateAsync(new FirmInfo()
                {
                    BatchId = request.batchCode,
                    CampanyImage = imgs,
                    CompanyName = null,
                    Address = null,
                    CampanyIntro = null,
                    FirmType = FirmType.深加工公司,
                    FoundingTime = null,
                    licenseNumber = null,
                    Principal = null,
                    Quantity = 0,
                });
            }
        }
    }

    public class DeepFirmImgDeleteCommandHandler : IRequestHandler<DeepFirmImgDeleteCommand, int>
    {
        private readonly IRepository<FirmInfo> _firminfo;

        public DeepFirmImgDeleteCommandHandler(IRepository<FirmInfo> firminfo)
        {
            _firminfo = firminfo;
        }

        public async Task<int> Handle(DeepFirmImgDeleteCommand request, CancellationToken cancellationToken)
        {
            var count = 0;
            var info = await _firminfo.GetValues(x => x.BatchId == request.batchCode&&x.FirmType==FirmType.深加工公司);
            var Imgs = info.FirstOrDefault().CampanyImage.Split(",").ToList();
            Imgs.RemoveAll(x => x == request.imgUrls);
            info.FirstOrDefault().CampanyImage = string.Join(",", Imgs);
            count += await _firminfo.UpdateAsync(info.FirstOrDefault());
            return count;
        }
    }
}
